﻿using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using MaternityHospitalPatients.Data;
using MaternityHospitalPatients.InputContract;
using MaternityHospitalPatients.Repository;

namespace MaternityHospitalPatients.Services
{
    public class ImportService
    {
        private readonly Regex _areaExtractor = new Regex("(\\d*)-(\\w*).*");

        public void Import(Report report, Action<int> progress)
        {
            ReportPageMaster_data[] data = report.Page.Master_data;
            int itemsCount =
                data.Sum(d => d.Detail_data.Sum(dd => dd.Subdetail_data.Sum(sdd => sdd.Master_data1.Length)));
            int currentItem = 0;
            var dataRepository = new DataRepository();
            foreach (ReportPageMaster_data reportPageMasterData in data)
            {
                Match matches = _areaExtractor.Match(reportPageMasterData.Detail_header.memo_8.T);
                var area = new Areas()
                               {
                                   ID = Convert.ToInt32(matches.Groups[1].Value),
                                   Title = matches.Groups[2].Value
                               };
                foreach (ReportPageMaster_dataDetail_data detailData in reportPageMasterData.Detail_data)
                {
                    foreach (ReportPageMaster_dataDetail_dataSubdetail_data subdetailData in detailData.Subdetail_data)
                    {
                        foreach (
                            ReportPageMaster_dataDetail_dataSubdetail_dataMaster_data1 masterData1 in
                                subdetailData.Master_data1)
                        {
                            var department = new Departments()
                                                 {
                                                     Title =
                                                         masterData1.memo_14.T.Replace("%0A", string.Empty)
                                                 };
                            var patientRecord = new PatientRecords
                                                    {
                                                        AreaID = area.ID,
                                                        DepartmentID = department.ID,
                                                        ID = Convert.ToInt32(masterData1.memo_12.T),
                                                        ReceiptDate =
                                                            DateTime.ParseExact(masterData1.memo_10.T, "dd.mm.yyyy",
                                                                                CultureInfo.InvariantCulture),
                                                        IssueDate =
                                                            DateTime.ParseExact(masterData1.memo_11.T, "dd.mm.yyyy",
                                                                                CultureInfo.InvariantCulture),
                                                        Name = masterData1.memo_13.T
                                                    };
                            dataRepository.SavePatientRecord(patientRecord, area, department);
                            progress((int) ((float) ++currentItem/itemsCount*100));
                        }
                    }
                }
            }
        }

        public IQueryable<PatientRecords> GetPatientRecord()
        {
            var dataRepository = new DataRepository();
            return dataRepository.GetPatientRecords();
        }

        public IQueryable<Debtors> GetDebtors()
        {
            var dataRepository = new DataRepository();
            return dataRepository.GetDebtors();
        }
    }
}